﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Data;
using System.Xml;
using System.Xml.Xsl;

public partial class KMrpt30 : ExtBaseRpt
{
    string mid;
    protected void Page_Load(object sender, EventArgs e)
    {
        this.arealist.GetStore().DataSource = db.DM_GetDateSet("select * from Bas_Area where Level = 1 order by areaname");
        this.txtseries.GetStore().DataSource = db.DM_GetDateSet("select distinct seriesname from km_v_mat");
        this.txtseason.GetStore().DataSource = db.DM_GetDateSet("select distinct seasonname from km_v_mat");
        this.txtyear.GetStore().DataSource = db.DM_GetDateSet("select distinct yearno from km_v_mat order by yearno");
        this.txtmatkind.GetStore().DataSource = db.DM_GetDateSet("select distinct kindname from km_v_mat");
        this.txtbigarea.GetStore().DataSource = mydb.DM_GetDateSet("select distinct bigareaname from [dbo].[Bas_RptAreaSet]");
        mid = Request.Params["mid"];
        if (!X.IsAjaxRequest)
        {
            //BuildGridPanel(this.GridPanel1, mid, Session["UserID"].ToString());
            this.txtbdate.Text = DateTime.Now.ToString();

            this.txtedate.Text = DateTime.Now.ToString();
            InsActLog(mid,"浏览");
            //this.txtbjhdate.Text = DateTime.Now.ToString("yyyy-MM-01");
            //this.txtejhdate.Text = DateTime.Now.ToString();
        }
    }

    [DirectMethod(Namespace = "CompanyX")]
    public void binStock(string area)
    {
        
    }
    protected void BtnQureyUser(object sender, DirectEventArgs e)
    {
        string bdate, edate;
        if (Convert.ToDateTime(txtbdate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
            bdate = "";
        else
            bdate = txtbdate.Text;

        if (Convert.ToDateTime(txtedate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
            edate = "";
        else
            edate = txtedate.Text;
        string bigarea = arealist.Text;
        string series = StrSplitParam(txtseries);
        string season = StrSplitParam(this.txtseason);
        string yearno = StrSplitParam(this.txtyear);
        string matkind = StrSplitParam(this.txtmatkind);
        string shortname = txtshortname.Text;
        string shop = StrSplit(this.selectshop.Text);
        DataSet ds = db.DM_GetDateSet("KM_PosSaleStockBySizeNew_Rpt '" + bdate + "','" + edate + "','" + txtbigarea.Value + "','" + bigarea + "','"
            + matkind + "','" + yearno + "','" + season + "','" + series + "','" + shortname + "','"+shop+"','"+txtitemname.Text+"','上装'");
        this.Store1.DataSource = ds;
        this.Store1.DataBind();
        //DynaGridResult(ds);
    }
    protected void BtnQureyUser2(object sender, DirectEventArgs e)
    {
        string bdate, edate;
        if (Convert.ToDateTime(txtbdate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
            bdate = "";
        else
            bdate = txtbdate.Text;

        if (Convert.ToDateTime(txtedate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
            edate = "";
        else
            edate = txtedate.Text;
        string bigarea = arealist.Text;
        string series = StrSplitParam(txtseries);
        string season = StrSplitParam(this.txtseason);
        string yearno = StrSplitParam(this.txtyear);
        string matkind = StrSplitParam(this.txtmatkind);
        string shortname = txtshortname.Text;
        string shop = StrSplit(this.selectshop.Text);
        DataSet ds = db.DM_GetDateSet("KM_PosSaleStockBySizeNew_Rpt '" + bdate + "','" + edate + "','" + txtbigarea.Value + "','" + bigarea + "','"
            + matkind + "','" + yearno + "','" + season + "','" + series + "','" + shortname + "','" + shop + "','" + txtitemname.Text + "','下装'");
        this.Store6.DataSource = ds;
        this.Store6.DataBind();
        //DynaGridResult(ds);
    }
    protected void BtnQureyUser3(object sender, DirectEventArgs e)
    {
        string bdate, edate;
        if (Convert.ToDateTime(txtbdate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
            bdate = "";
        else
            bdate = txtbdate.Text;

        if (Convert.ToDateTime(txtedate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
            edate = "";
        else
            edate = txtedate.Text;
        string bigarea = arealist.Text;
        string series = StrSplitParam(txtseries);
        string season = StrSplitParam(this.txtseason);
        string yearno = StrSplitParam(this.txtyear);
        string matkind = StrSplitParam(this.txtmatkind);
        string shortname = txtshortname.Text;
        string shop = StrSplit(this.selectshop.Text);
        DataSet ds = db.DM_GetDateSet("KM_PosSaleStockBySizeNew_Rpt '" + bdate + "','" + edate + "','" + txtbigarea.Value + "','" + bigarea + "','"
            + matkind + "','" + yearno + "','" + season + "','" + series + "','" + shortname + "','" + shop + "','" + txtitemname.Text + "','鞋'");
        this.Store7.DataSource = ds;
        this.Store7.DataBind();
        //DynaGridResult(ds);
    }

    #region 动态加载报表显示结果
    private void DynaGridResult(DataSet ds)
    {
        if (ds == null) return;

        Store store = this.Store1;
        GridPanel grid = this.GridPanel1;
        store.Reader.Clear();
        grid.ColumnModel.Columns.Clear();
        store.Model.Clear();
       
        Model md = new Model();
        List<ColumnBase> list = new List<ColumnBase>();

        DataTable dt = ds.Tables[0];
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            //根据结果集创建Store
            ModelField field = new ModelField();
            field.Name = dt.Columns[i].ToString();
            md.Fields.Add(field);
            //创建grid colums
            Column cl = new Column();
            cl.Text = dt.Columns[i].ToString();
            cl.DataIndex = dt.Columns[i].ToString();
            list.Add(cl);
        }
        store.Model.Add(md);
        store.DataSource = ds;
        store.DataBind();
        grid.ColumnModel.Columns.Add(list);
        grid.Render();
    }
    #endregion
    protected void RefreshDataSet(object sender, StoreReadDataEventArgs e)
    {
        BtnQureyUser(null,null);
    }
    protected void Store1_Submit(object sender, StoreSubmitDataEventArgs e)
    {
        //导出EXCEL
        ExtGridToExcel(mid,"xls",e.Xml,this.Response,this.Title);
    }
    protected void ToExcel(object sender, EventArgs e)
    {
        InsActLog(mid, "导出");
        string json = this.Hidden1.Value.ToString();
        StoreSubmitDataEventArgs eSubmit = new StoreSubmitDataEventArgs(json, null);
        XmlNode xml = eSubmit.Xml;

        this.Response.Clear();
        this.Response.ContentType = "application/vnd.ms-excel";
        this.Response.AddHeader("Content-Disposition", "attachment; filename=" + this.Title + ".xls");

        XslCompiledTransform xtExcel = new XslCompiledTransform();

        xtExcel.Load(Server.MapPath("Excel.xsl"));
        xtExcel.Transform(xml, null, this.Response.OutputStream);
        this.Response.End();
    }
    protected void BtnOutExcel(object sender, DirectEventArgs e)
    {
        InsActLog(mid, "导出");
        string bdate, edate;
        if (Convert.ToDateTime(txtbdate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
            bdate = "";
        else
            bdate = txtbdate.Text;

        if (Convert.ToDateTime(txtedate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
            edate = "";
        else
            edate = txtedate.Text;
        string bigarea = arealist.Text;
        string series = StrSplitParam(txtseries);
        string season = StrSplitParam(this.txtseason);
        string yearno = StrSplitParam(this.txtyear);
        string matkind = StrSplitParam(this.txtmatkind);
        string shortname = txtshortname.Text;
        string shop = StrSplit(this.selectshop.Text);
        DataSet ds = db.DM_GetDateSet("KM_PosSaleStockBySizeNew_Rpt '" + bdate + "','" + edate + "','" + txtbigarea.Value + "','" + bigarea + "','"
            + matkind + "','" + yearno + "','" + season + "','" + series + "','" + shortname + "','" + shop + "','" + txtitemname.Text + "','上装'");
        this.Store1.DataSource = ds;
        this.Store1.DataBind();
        string name = DateTime.Now.ToString("yyMMddHHmmss") + this.Title + "(上装)";
        string path = Server.MapPath("./UploadFile/") + name + ".xls";
        if (DataSetToExcel(mid, ds, name, path, false))
        {
            ShowBox("下载地址：" + "<a href='http://219.135.99.176:58285/UploadFile/" + name + ".xls'>" + name + "</a>");
        }
        else
        {
            ShowBox("生成失败！");
        }
    }
    protected void BtnOutExcel2(object sender, DirectEventArgs e)
    {
        InsActLog(mid, "导出");
        string bdate, edate;
        if (Convert.ToDateTime(txtbdate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
            bdate = "";
        else
            bdate = txtbdate.Text;

        if (Convert.ToDateTime(txtedate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
            edate = "";
        else
            edate = txtedate.Text;
        string bigarea = arealist.Text;
        string series = StrSplitParam(txtseries);
        string season = StrSplitParam(this.txtseason);
        string yearno = StrSplitParam(this.txtyear);
        string matkind = StrSplitParam(this.txtmatkind);
        string shortname = txtshortname.Text;
        DataSet ds = db.DM_GetDateSet("KM_PosSaleStockBySizeNew_Rpt '" + bdate + "','" + edate + "','" + txtbigarea.Value + "','" + bigarea + "','"
            + matkind + "','" + yearno + "','" + season + "','" + series + "','" + shortname + "','下装'");
        string name = DateTime.Now.ToString("yyMMddHHmmss") + this.Title + "(下装)";
        string path = Server.MapPath("./UploadFile/") + name + ".xls";
        if (DataSetToExcel(mid, ds, name, path, false))
        {
            ShowBox("下载地址：" + "<a href='http://219.135.99.176:58285/UploadFile/" + name + ".xls'>" + name + "</a>");
        }
        else
        {
            ShowBox("生成失败！");
        }
    }
    protected void BtnOutExcel3(object sender, DirectEventArgs e)
    {
        InsActLog(mid, "导出");
        string bdate, edate;
        if (Convert.ToDateTime(txtbdate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
            bdate = "";
        else
            bdate = txtbdate.Text;

        if (Convert.ToDateTime(txtedate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
            edate = "";
        else
            edate = txtedate.Text;
        string bigarea = arealist.Text;
        string series = StrSplitParam(txtseries);
        string season = StrSplitParam(this.txtseason);
        string yearno = StrSplitParam(this.txtyear);
        string matkind = StrSplitParam(this.txtmatkind);
        string shortname = txtshortname.Text;
        DataSet ds = db.DM_GetDateSet("KM_PosSaleStockBySizeNew_Rpt '" + bdate + "','" + edate + "','" + txtbigarea.Value + "','" + bigarea + "','"
            + matkind + "','" + yearno + "','" + season + "','" + series + "','" + shortname + "','鞋'");
        string name = DateTime.Now.ToString("yyMMddHHmmss") + this.Title + "(鞋)";
        string path = Server.MapPath("./UploadFile/") + name + ".xls";
        if (DataSetToExcel(mid, ds, name, path, false))
        {
            ShowBox("下载地址：" + "<a href='http://219.135.99.176:58285/UploadFile/" + name + ".xls'>" + name + "</a>");
        }
        else
        {
            ShowBox("生成失败！");
        }
    }
}